package com.dhcc.bpm.common.utils;

import com.dhcc.bpm.common.constant.FilePathConstant;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

@Component
public class ImgUtil {

    private static final Logger log = LoggerFactory.getLogger(ImgUtil.class);

    private static String imgPrefix;

	@Value("${bpm.file-public-path}")
    private void setImgPrefix(String imgUrl){
		 imgPrefix = imgUrl + FilePathConstant.FILEPATH;
	}
	/**
	 * 将图片转换成二进制
	 * imgPath 图片路径
	 * imgName 图片名字
	 * @return
	 * @throws IOException 
	 */
	@SuppressWarnings("restriction")
	public static String getImageBinary(String imgPath,String imgName) throws IOException {
        File f = new File(imgPrefix + imgPath + imgName);
        log.info("图片路径：" +f.getPath());
        BufferedImage bi = null;
		ByteArrayOutputStream baos = null;
		try {
			bi = ImageIO.read(f);
			baos = new ByteArrayOutputStream();
			ImageIO.write(bi, "jpg", baos);  //经测试转换的图片是格式这里就什么格式，否则会失真
			byte[] bytes = baos.toByteArray();
			String imgText = Base64.encodeBase64String(bytes);
			//base64进行编码
			java.util.Base64.Encoder encoder = java.util.Base64.getEncoder();
			byte[] textByte = null;
			try {
				textByte  = imgText.getBytes("UTF-8");
			} catch (UnsupportedEncodingException e) {
				log.error("异常信息：{}", e);
			}
			String imgCode = encoder.encodeToString(textByte);
			log.info("图片编码：" +imgCode);
			return imgCode;
		//	return new BASE64Encoder().encodeBuffer(bytes).trim();
		} catch (Exception e) {
		    log.info("异常:{}" ,e);
			throw e;
		}finally {
			if(baos != null){
				try {
					baos.close();
				}catch (IOException e){
					log.info("异常:{}" ,e);
				}
			}
		}
	}

	/**
	 * @Description 根据路径返回base64编码
	 * @Author sjm
	 * @Date 22:07 2020/4/20 0020
	 * @param imgPath
	 * @return java.lang.String
	 **/
	@SuppressWarnings("restriction")
	public static String ImageToBase64(String imgPath) throws Exception {
		File f = new File(imgPrefix + imgPath);
		log.info("图片路径：" +f.getPath());
		BufferedImage bi;
		ByteArrayOutputStream baos = null;
		try {
			bi = ImageIO.read(f);
			baos = new ByteArrayOutputStream();
			ImageIO.write(bi, "jpg", baos);  //经测试转换的图片是格式这里就什么格式，否则会失真
			byte[] bytes = baos.toByteArray();

			return new BASE64Encoder().encodeBuffer(bytes).trim();
		} catch (Exception e) {
			log.info("异常:{}" ,e);
			throw e;
		}finally {
			if(baos!=null){
				try {
					baos.close();
				}catch (IOException e){
					log.info("异常:{}" ,e);
				}
			}
		}
	}

	/**
	 * 将二进制转换为图片
	 * 
	 * @param 
	 * base64String 
	 * imgPath 图片路径
	 * imgName 图片名字
	 * @throws IOException 
	 */
	public static void base64StringToImage(String base64String,String imgPath,String imgName) throws Exception {
		ByteArrayInputStream bais = null;
		try {
			@SuppressWarnings("restriction")
			byte[] bytes1 = new BASE64Decoder().decodeBuffer(base64String);
 
			bais = new ByteArrayInputStream(bytes1);
			BufferedImage bi1 = ImageIO.read(bais);
			File w2 = new File(imgPath+imgName);// 可以是jpg,png,gif格式
			ImageIO.write(bi1, "jpg", w2);// 不管输出什么格式图片，此处不需改动
		} catch (Exception e) {
			log.info("异常:{}" ,e);
			throw e;
		}finally {
			if (bais!=null){
				try {
					bais.close();
				}catch (IOException e){
					log.info("异常:{}" ,e);
				}
			}
		}
	}

}
